﻿using FileData.Currency.TypeConvert;
using FileData.Utils.Currency;
using FileData.Utils.Currency.FileUtils;
using System;

namespace FileData.DataUtils.ViewUtils
{
    internal class RuleTool
    {
        string Flag = "==r=e=g=t=e=x=t=";
        string LocalFlag = "col01wsg";
        public void CreateFile(string FilePath, string Text, string Keys,bool ClearFlag) {

            Text = new DESCryptoTool().Encrypt(Text, LocalFlag);
            if (Keys != "")
                Text = new DESCryptoTool().Encrypt(Text, Keys);
            CreateFile(FilePath,Text,ClearFlag);
        }
        public string ReadFile(string FilePath,string Keys) {
            string Content = ReadFile(FilePath);
            Content = TextHelper.ReplaceText(Content, "=====.*?=====","");
            Console.WriteLine(Content);
            if (Content == "") {
                Console.WriteLine("Data  is null");
                return ""; 
            }

            if(Keys!="")
                Content = new DESCryptoTool().Decrypt(Content.Trim(), Keys);
            Content = new DESCryptoTool().Decrypt(Content.Trim(), LocalFlag);
            return Content;

        }

        /// <summary>
        /// 获取文件并且阅读
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public string ReadFile(string FilePath)
        {
            string Content = "";
            if (FilePath == "" || !FileHelper.FileExits(FilePath))
            {
                Console.WriteLine("文件不存在");
                return Content;
            }
            string FileContent = new HexConvert().FileToHex(FilePath);

         
            foreach (string Line in TextHelper.RegexTextList(FileContent, new HexConvert().StrToHex(Flag) + ".*?" + new HexConvert().StrToHex(Flag))) {
                Content += Line;
            }
          
            Content = new HexConvert().HexStrToStrinig(Content).Replace(Flag, "");
          
            return Content;
        }
        /// <summary>
        /// 清除某个数据块中的数据
        /// </summary>
        /// <param name="FilePath"></param>
        public void ClearFile(string FilePath) {
            string FileContent = new HexConvert().FileToHex(FilePath);
            FileContent = TextHelper.ReplaceText(FileContent, new HexConvert().StrToHex(Flag) + ".*?" + new HexConvert().StrToHex(Flag),"");

            new ByteConvert().ByteToFile(new HexConvert().HexStrTobyte(FileContent), FilePath);
        }

        /// <summary>
        /// 读取文件并创建
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="Text"></param>
        /// <param name="ClearFlag">是否处理旧数据</param>
        public void CreateFile(string FilePath, string Text,bool ClearFlag)
        {
            if (ClearFlag)
                ClearFile(FilePath);

            byte[] FileByte = new ByteConvert().FileToBytes(FilePath);
           
            Text = ReadFile(FilePath) + string.Format("====={0}=====",DateTime.Now.ToString()) + Text;
      
            string SaveText = Flag + Text + Flag;
            byte[] StrByte = new ByteConvert().StrTobyte(SaveText);
            byte[] Temp = new ByteConvert().ByteMerge(FileByte, StrByte);

            new ByteConvert().ByteToFile(Temp, FilePath);
        }
    }
}